package com.ketai.service.impl;

import com.ketai.dao.CheckNumberDao;
import com.ketai.dao.impl.CheckNumberDaoImpl;
import com.ketai.service.CheckNumberService;
import com.ketai.vo.StatisticsVo;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:胡远博
 * @Description:
 * @Date:Create In 2022-08-11 2:15
 **/
public class CheckNumberServiceImpl implements CheckNumberService {
    CheckNumberDao checkNumberDao = new CheckNumberDaoImpl();

    @Override
    public int selectUserCount() {
        try {
            return checkNumberDao.selectUserCount();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int selectOrganCount() {
        try {
            return checkNumberDao.selectOrganCount();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int selectActivityCount() {
        try {
            return checkNumberDao.selectActivityCount();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List selectSubtotal() {
        try {
            return checkNumberDao.selectSubtotal();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<StatisticsVo> getStatistics() {
        //先处理本周的日期
        LocalDateTime.now().getDayOfWeek();
        //活动统计
        String activityCountSql = "select count(id) as count from c_activity";
        //今日
        String todayActivityCountSql = activityCountSql + " where to_days(time)=to_days(now()) ";
        //昨天
        String yesterdayActivityCountSql = activityCountSql + " where TO_DAYS(NOW()) - TO_DAYS(time) = 1  ";
        //本周
        String currentWeekActivityCountSql = activityCountSql + " where YEARWEEK(date_format(time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        //本月
        String currentMonthActivityCountSql = activityCountSql + " where DATE_FORMAT(time, '%Y%m') = DATE_FORMAT(now( ) , '%Y%m') ";

        //用户
        String userCountSql = "select count(id) as count from c_user ";
        //今天
        String todayUserCountSql = userCountSql + " where to_days(create_time)=to_days(now()) ";
        //昨天
        String yesterdayUserCountSql = userCountSql + " where TO_DAYS(NOW()) - TO_DAYS(create_time) = 1  ";
        //本周
        String currentWeekUserCountSql = userCountSql + " where YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        //本月
        String currentMonthUserCountSql = userCountSql + " where DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(now( ) , '%Y%m')  ";

        //报名
        String applyCountSql = "select count(id) as count from apply ";
        //今天
        String todayApplyCountSql = applyCountSql+"where to_days(application_time)=to_days(now())";
        //昨天
        String yesterdayApplyCountSql = applyCountSql+"where TO_DAYS(NOW()) - TO_DAYS(application_time) = 1 ";
        //本周
        String currentWeekApplyCountSql = applyCountSql+"where YEARWEEK(date_format(application_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        //当月
        String currentMonthApplyCountSql = applyCountSql+" where DATE_FORMAT(application_time, '%Y%m') = DATE_FORMAT(now( ) , '%Y%m')  ";

        List<StatisticsVo> statisticsVos = new ArrayList<>(4);
        try {
            StatisticsVo statisticsVo = new StatisticsVo();
            //总数
            statisticsVo.setActivityCount(checkNumberDao.getCountByTpe(activityCountSql));
            statisticsVo.setUserCount(checkNumberDao.getCountByTpe(userCountSql));
            statisticsVo.setSignUpCount(checkNumberDao.getCountByTpe(applyCountSql));
            statisticsVos.add(statisticsVo);
        //    今天
            StatisticsVo todayStatisticsVo = new StatisticsVo();
            todayStatisticsVo.setActivityCount(checkNumberDao.getCountByTpe(todayActivityCountSql));
            todayStatisticsVo.setUserCount(checkNumberDao.getCountByTpe(todayUserCountSql));
            todayStatisticsVo.setSignUpCount(checkNumberDao.getCountByTpe(todayApplyCountSql));
            statisticsVos.add(todayStatisticsVo);
        //    昨天
            StatisticsVo yesterdayStatisticsVo = new StatisticsVo();
            yesterdayStatisticsVo.setActivityCount(checkNumberDao.getCountByTpe(yesterdayActivityCountSql));
            yesterdayStatisticsVo.setUserCount(checkNumberDao.getCountByTpe(yesterdayUserCountSql));
            yesterdayStatisticsVo.setSignUpCount(checkNumberDao.getCountByTpe(yesterdayApplyCountSql));
            statisticsVos.add(yesterdayStatisticsVo);

            //本周
            StatisticsVo currentWeekStatisticsVo = new StatisticsVo();
            currentWeekStatisticsVo.setActivityCount(checkNumberDao.getCountByTpe(currentWeekActivityCountSql));
            currentWeekStatisticsVo.setUserCount(checkNumberDao.getCountByTpe(currentWeekUserCountSql));
            currentWeekStatisticsVo.setSignUpCount(checkNumberDao.getCountByTpe(currentWeekApplyCountSql));
            statisticsVos.add(currentWeekStatisticsVo);

            //本月
            StatisticsVo currentMonthStatisticsVo = new StatisticsVo();
            currentMonthStatisticsVo.setActivityCount(checkNumberDao.getCountByTpe(currentMonthActivityCountSql));
            currentMonthStatisticsVo.setUserCount(checkNumberDao.getCountByTpe(currentMonthUserCountSql));
            currentMonthStatisticsVo.setSignUpCount(checkNumberDao.getCountByTpe(currentMonthApplyCountSql));
            statisticsVos.add(currentMonthStatisticsVo);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return statisticsVos;
    }
}
